package com.szqhyxj.youxiangju.biz.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.MerchantContainerActivity;
import com.szqhyxj.youxiangju.biz.register.RegisterActivity;
import com.szqhyxj.youxiangju.biz.set.forgetpassword.ForgetPasswordActivity;
import com.szqhyxj.youxiangju.sys.util.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity implements LoginContract.View {

    private LoginContract.Presenter loginPresenter;

    public static void start(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, LoginActivity.class);
        activity.startActivity(intent);
    }

    public static abstract class SimpleTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.login)
    TextView login;
    @BindView(R.id.forget_password)
    TextView forgetPassword;
    @BindView(R.id.register)
    TextView register;

    Boolean isclick = false;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        loginPresenter = new LoginPresenter( this);
        initViews();
        initEvents();
    }


    private void initViews(){
        StringUtils.setEditTextHintSize(this,phone,"请输入手机号",11);
        StringUtils.setEditTextHintSize(this,password,"请输入密码（6-16位字母与数字组合）",11);
    };

    private void initEvents(){
        phone.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                loginPresenter.setAccount(s.toString());
            }
        });
        password.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                loginPresenter.setPassword(s.toString());
            }
        });
    }

    @Override
    public void navToRegister() {
        RegisterActivity.start(this);
    }

    @Override
    public void navToForgetPassword() {
        ForgetPasswordActivity.start(this);
    }

    @Override
    public void showLoginEnable() {
        login.setEnabled(true);
        login.setClickable(true);
        login.setBackgroundResource(R.mipmap.login_btn_pre_denglu);
    }

    @Override
    public void showLoginDisable() {
        login.setEnabled(false);
        login.setClickable(false);
        login.setBackgroundResource(R.mipmap.login_btn_denglu);
    }

    @Override
    public void showLoginSuccess() {
        MerchantContainerActivity.start(this);
    }

    @Override
    public void showLoginFail(String message) {
        isclick = false;
        Toast.makeText(this,message,Toast.LENGTH_LONG).show();
    }

    @OnClick({R.id.login, R.id.forget_password, R.id.register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login:
                if (isclick==false){
                    loginPresenter.login();
                    isclick = true;
                }else{
                    Toast.makeText(LoginActivity.this,"正在登录，请稍等",Toast.LENGTH_LONG).show();
                }

                break;
            case R.id.forget_password:
                loginPresenter.forgetPassword();
                break;
            case R.id.register:
                loginPresenter.register();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loginPresenter.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            MerchantContainerActivity.start(this);
        }
        return super.onKeyDown(keyCode, event);
    }
}